﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Web.UI;

namespace Lilium.Web.Mvc.AjaxControlToolkit
{
	/// <summary>
	/// This extender allows you to dynamically populate the innerHTML of a control based on a 
	/// web service callback.
	/// </summary>
	[RequiredResource(typeof(CommonToolkitScripts))]
	[ToolkitScriptReference("DynamicPopulate.DynamicPopulateBehavior.js")]
	public class DynamicPopulateExtender: ToolkitExtenderBase
	{
		public DynamicPopulateExtender(string elementID)
			: base("AjaxControlToolkit.DynamicPopulateBehavior", elementID)
		{
		}

		#region public bool ClearContentsDuringUpdate

		private bool m_ClearContentsDuringUpdate = true;

		/// <summary>
		/// Whether or not we should clear the HTML contents of the
		/// target element when an update begins
		/// </summary>
		[DefaultValue(true)]
		[ExtenderControlProperty]
		[Category("Behavior")]
		public bool ClearContentsDuringUpdate { [DebuggerNonUserCodeAttribute] get { return m_ClearContentsDuringUpdate; } [DebuggerNonUserCodeAttribute] set { m_ClearContentsDuringUpdate = value; } }

		#endregion

		#region public string ContextKey

		private string m_ContextKey;

		/// <summary>
		/// An key that you can pass to the web service call, such as an ID.  This is optional.
		/// </summary>
		[DefaultValue("")]
		[ExtenderControlProperty]
		[Category("Behavior")]
		public string ContextKey { [DebuggerNonUserCodeAttribute] get { return m_ContextKey ?? ""; } [DebuggerNonUserCodeAttribute] set { m_ContextKey = value; } }

		#endregion

		#region public string PopulateTriggerControlID

		private string m_PopulateTriggerControlID;

		/// <summary>
		/// The ID of a control to trigger the population of the target.  The population will
		/// be triggered by this controls "click" event.
		/// </summary>
		[ExtenderControlProperty]
		//[IDReferenceProperty(typeof(Control))]
		[ClientPropertyName("PopulateTriggerID")]
		[Category("Behavior")]
		[SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Following ASP.NET AJAX pattern")]
		public string PopulateTriggerControlID { [DebuggerNonUserCodeAttribute] get { return m_PopulateTriggerControlID ?? ""; } [DebuggerNonUserCodeAttribute] set { m_PopulateTriggerControlID = value; } }

		#endregion

		#region public string ServiceMethod

		private string m_ServiceMethod;

		/// <summary>
		/// The method name of the web service of page method to call.
		/// </summary>
		[ExtenderControlProperty]
		[DefaultValue("")]
		[Category("Behavior")]
		[SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", Justification = "Assembly is not localized")]
		public string ServiceMethod
		{
			[DebuggerNonUserCodeAttribute]
			get { return m_ServiceMethod ?? ""; }
			[DebuggerNonUserCodeAttribute]
			set
			{
				if (!string.IsNullOrEmpty(CustomScript))
				{
					throw new InvalidOperationException("ServiceMethod can not be set if a CustomScript is set.");
				}
				m_ServiceMethod = value;
			}
		}

		#endregion

		#region public string ServicePath

		private string m_ServicePath;

		/// <summary>
		/// The path of the web service to call, or if this is blank, a page method will be
		/// called instead.
		/// </summary>        
		[ExtenderControlProperty]
		[UrlProperty]
		[TypeConverter(typeof(ServicePathConverter))]
		[Category("Behavior")]
		public string ServicePath { [DebuggerNonUserCodeAttribute] get { return m_ServicePath ?? ""; } [DebuggerNonUserCodeAttribute] set { m_ServicePath = value; } }

		/// <summary>
		/// Prevent serialization of the service path if no ServiceMethod was supplied
		/// </summary>
		/// <returns>Whether or not the service path should be serialized</returns>
		private bool ShouldSerializeServicePath()
		{
			return !string.IsNullOrEmpty(ServiceMethod);
		}

		#endregion

		#region public string UpdatingCssClass

		private string m_UpdatingCssClass;

		/// <summary>
		/// A CSS style to apply while the update is in progress
		/// </summary>
		[ExtenderControlProperty]
		[DefaultValue("")]
		[Category("Behavior")]
		public string UpdatingCssClass { [DebuggerNonUserCodeAttribute] get { return m_UpdatingCssClass ?? ""; } [DebuggerNonUserCodeAttribute] set { m_UpdatingCssClass = value; } }

		#endregion

		#region public string CustomScript

		private string m_CustomScript;

		/// <summary>
		/// A CustomScript can be used to eval a javascript function that will return a string to populate the control.  This script method
		/// must return a string and will be called instead of the Service or Page method.
		/// </summary>
		[ExtenderControlProperty]
		[DefaultValue("")]
		[Category("Behavior")]
		[SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", Justification = "Assembly is not localized")]
		public string CustomScript
		{
			[DebuggerNonUserCodeAttribute]
			get { return m_CustomScript ?? ""; }
			[DebuggerNonUserCodeAttribute]
			set
			{
				if (!string.IsNullOrEmpty(ServiceMethod))
				{
					throw new InvalidOperationException("CustomScript can not be set if a ServiceMethod is set.");
				}
				m_CustomScript = value;
			}
		}

		#endregion

		#region public bool CacheDynamicResults

		private bool m_CacheDynamicResults;

		/// <summary>
		/// Whether the results of the dynamic population should be cached and
		/// not fetched again after the first load
		/// </summary>
		[ExtenderControlProperty]
		[ClientPropertyName("cacheDynamicResults")]
		[DefaultValue(false)]
		[Category("Behavior")]
		public bool CacheDynamicResults { [DebuggerNonUserCodeAttribute] get { return m_CacheDynamicResults; } [DebuggerNonUserCodeAttribute] set { m_CacheDynamicResults = value; } }

		#endregion
		/* Сейчас не используется, но это не плохая идея добавить такую проверку.
		/// <summary>
		/// Check if the properties have been set correctly
		/// </summary>
		/// <param name="throwException">Whether exceptions should be thrown on violations</param>
		/// <returns>True if the properties have been set correctly, false if not</returns>
		[SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", Justification = "Assembly is not localized")]
		protected override bool CheckIfValid(bool throwException)
		{
			if (string.IsNullOrEmpty(CustomScript) && string.IsNullOrEmpty(ServiceMethod))
			{
				if (throwException)
				{
					throw new InvalidOperationException("CustomScript or ServiceMethod must be set.");
				}
				return false;
			}
			return base.CheckIfValid(throwException);
		}
		*/
	}
}